package com.wskj.library.dao.impl;

import com.wskj.library.pojo.*;

import com.wskj.library.dao.*;

import java.util.Date;
import java.util.List;

/**
 * borrowInfo dao接口类实现类
 * 2018-03-22 T148
 */ 
public class BorrowInfoDaoImpl extends BaseDao implements BorrowInfoDao{

	 @Override
	 public int addBorrowInfo(BorrowInfo  borrowInfo){
		 String sql="insert into borrowInfo  values  ()";
		 Object[] params= {};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public int deleteById(int id){
		 String sql="delete from borrowInfo  where bid=? ";
		 Object[] params= {id};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public int updateBorrowInfo(BorrowInfo borrowInfo){
		 String sql="update borrowInfo set ";
		 Object[] params= {};
		 return super.executeUpdate(sql, params);
	 }
	 @Override
	 public BorrowInfo getBorrowInfoById(int id){
		 String sql="select * from borrowInfo where bid=? ";
		 Object[] params= {id};
		 return super.queryOne(sql, params,BorrowInfo.class);
	 }
	 @Override
	 public  List<BorrowInfo>  getAllBorrowInfos(){
		 String sql="select * from borrowInfo";
		 Object[] params= null;
		 return super.queryAll(sql, params, BorrowInfo.class);
	 }
	
	@Override
	public long getCounts(String bname, String rname, Date borrowTime) {
			
		String sql="select  COUNT(1) " + 
				" from borrowinfo  bw  " + 
				"  inner join bookinfo b " + 
				"  on bw.bookid=b.bid  " + 
				"  inner join userinfo u " + 
				"  on bw.rid=u.uid " + 
				"  where  u.uname like CONCAT('%',?,'%') " + 
				"  and b.bname like CONCAT('%',?,'%') " + 
				"  and bw.btime <? ";
		Object[] params= {rname,bname,borrowTime};
		return super.getCounts(sql, params);
	}
	
	
	
	@Override
	public List<BorrowInfo> getBorrowsWithPage(String bname, String rname, Date borrowTime, int index, int pageSize) {
		
		String sql="select  bw.bid,bw.bookid,bw.rid,bw.btime,IFNULL(bw.retime,NOW()) retime,"
				+ "bname,uname,IF((bw.netime-IFNULL(bw.retime,NOW()))>=0,'正常','延期') stus " + 
				"  from borrowinfo  bw  " + 
				"  inner join bookinfo b " + 
				"  on bw.bookid=b.bid  " + 
				"  inner join userinfo u " + 
				"  on bw.rid=u.uid " + 
				"  where  u.uname like CONCAT('%',?,'%') " + 
				"  and b.bname like CONCAT('%',?,'%') " + 
				"  and bw.btime <? "
				+ " limit ?,?";
		Object[] params= {rname,bname,borrowTime,(index-1)*pageSize,pageSize};
		return super.queryAll(sql, params, BorrowInfo.class);
	}
	
}

